package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import com.sun.media.jfxmedia.logging.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class PaymentController {

    @Resource
    PaymentService paymentService;

    @Value("${server.port}")
    private String port;

    @PostMapping("/payment/create")
    public CommonResult create(@RequestBody Payment payment){
        int result = paymentService.create(payment);
        if(result > 0){
            return new CommonResult(200,"操作成功：服务提供端口："+port,result);
        }else{
            return new CommonResult(500,"操作失败：服务提供端口："+port,result);
        }
    }
    @GetMapping("/payment/get/{id}")
    @ResponseBody
    public CommonResult get(@PathVariable Long id){
        Payment payment = paymentService.getPaymentById(id);
        if(null != payment){
            return new CommonResult(200,"操作成功：服务提供端口："+port,payment);
        }else{
            return new CommonResult(500,"操作失败：服务提供端口："+port,null);
        }
    }

    /**
     * 验证自己实现的负载均衡
     * @return
     */
    @GetMapping("/payment/lb")
    public String getPaymentLb(){
        return port;
    }
}
